package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityItemBinding;
import flc.ast.fragment.TabFragment;
import stark.common.basic.event.EventStatProxy;

public class ItemActivity extends BaseAc<ActivityItemBinding> {

    private List<Fragment> fragments = new ArrayList<>();
    private String[] tabTitle = new String[]{};

    @Override
    protected int onCreate() {
        return R.layout.activity_item;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivItemBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected boolean isStatusBarTextDark() {
        return false;
    }

    @Override
    protected void initData() {
        tabTitle = new String[]{getString(R.string.item_type1), getString(R.string.item_type2), getString(R.string.item_type3)};
        setTabData();
    }

    private void setTabData() {
        List<String> list = new ArrayList<>();
        list.add(AppConfig.ITEM_DATA1);
        list.add(AppConfig.ITEM_DATA2);
        list.add(AppConfig.ITEM_DATA3);
        for (int i = 0; i < list.size(); i++) {
            fragments.add(new TabFragment().newInstance(list.get(i)));
            mDataBinding.tlItem.addTab(mDataBinding.tlItem.newTab().setText(tabTitle[i]));
        }
        mDataBinding.vpItem.setAdapter(new ItemActivity.ArticleAdapter(getSupportFragmentManager()));
        mDataBinding.tlItem.setupWithViewPager(mDataBinding.vpItem);
        //给tab每个item设置值
        for (int i = 0; i < mDataBinding.tlItem.getTabCount(); i++) {
            TabLayout.Tab tab = mDataBinding.tlItem.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        // 设置默认第一个被选中
        View view = mDataBinding.tlItem.getTabAt(0).getCustomView();
        TextView tv = view.findViewById(R.id.tvTab);
        tv.setTextColor(Color.parseColor("#FF9511"));
        tv.setTextSize(18);
        //设置TabLayout点击事件
        mDataBinding.tlItem.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTab);
                tv.setTextColor(Color.parseColor("#FF9511"));
                tv.setTextSize(18);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTab);
                tv.setTextColor(Color.parseColor("#8A8A8A"));
                tv.setTextSize(16);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private class ArticleAdapter extends FragmentStatePagerAdapter {
        public ArticleAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitle[position];
        }
    }

    //自定义Tab布局
    private View getTabView(int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_tab, null);
        TextView text = view.findViewById(R.id.tvTab);
        text.setText(tabTitle[i]);
        text.setTextColor(Color.parseColor("#8A8A8A"));
        text.setTextSize(16);
        return view;
    }

}